﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Silverlight.Weblog.UI.Web.AppCode
{
    public class RemoveWWWModule : IHttpModule
    {
        #region IHttpModule Members

        public void Dispose()
        {

        }

        private static Regex regex = new Regex("(http|https)://www\\.",
            RegexOptions.IgnoreCase | RegexOptions.Compiled);

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;

            Uri url = application.Context.Request.Url;

            bool hasWWW = regex.IsMatch(url.ToString());

            if (hasWWW)
            {
                String newUrl = regex.Replace(url.ToString(),
                    String.Format("{0}://", url.Scheme));

                //application.Context.Response.Redirect(newUrl);

                application.Context.Response.Status = "301 Moved Permanently";
                application.Context.Response.AddHeader("Location", newUrl);
                application.Context.Response.End();
            }
        }

        #endregion
    }
}
